<?php

namespace app\master\controller;

use think\Db;
use think\Exception;

class Pricemanage extends Base
{
    //驾驶员价格设置
    public function driveprice()
    {
        if (request()->isGet()) {
            return $this->fetch('driveprice');
        }
        $page = input("page");
        $limit = input("limit");
        $where = [
            'd.type' => 1,
        ];
        $list = model('Drivelevel')->getDrivePrice($where, $page, $limit);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['drivelevel_price'] = $list['data'][$k]['drivelevel_price'] / 100;
            $list['data'][$k]['drivelevel_priceday'] = $list['data'][$k]['drivelevel_priceday'] / 100;
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //清空驾驶员价格
    public function delprice()
    {
        $drivelevel_id = input('drivelevel_id');
        $res = db('drivelevel')->where('drivelevel_id', $drivelevel_id)->update(['drivelevel_price' => null, 'drivelevel_priceday' => null]);
        if ($res) $this->ajaxReturn(200, "清空成功", url("Pricemanage/driveprice"));
        else $this->ajaxReturn(603, "清空失败");
    }

    //添加驾驶员等级
    public function adddriveprice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $data = db('driving_licence')->where('status', 1)->select();
            $this->assign('data', $data);
            return $this->fetch("adddriveprice");
        }
        $drivelevel_name = input('driving_licence_id');
        $drivelevel_price = input('drivelevel_price');
        $drivelevel_priceday = input('drivelevel_priceday');
        $count = db('drivelevel')->where(['driving_licence_id' => $drivelevel_name, 'type' => 1])->count();
        if ($count) $this->ajaxReturn(603, "驾驶证等级已存在！");
        $drivelevel = [
            "driving_licence_id" => $drivelevel_name,
            "drivelevel_price" => $drivelevel_price * 100,
            "drivelevel_priceday" => $drivelevel_priceday * 100,
        ];
        $update = db("drivelevel")->insert($drivelevel);
        if ($update) $this->ajaxReturn(200, "添加成功");
        else $this->ajaxReturn(603, "添加失败");

    }

    //修改驾驶员租金
    public function updprice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $info = db("drivelevel")->find(input("drivelevel_id"));
            $info['drivelevel_price'] = $info['drivelevel_price'] / 100;
            $info['drivelevel_priceday'] = $info['drivelevel_priceday'] / 100;
            $data = db('driving_licence')->where('status', 1)->select();
            $this->assign('data', $data);
            $this->assign("info", $info);
            return $this->fetch("updprice");
        }
        $data = input('post.');
        $upd = [
            "driving_licence_id" => $data['driving_licence_id'],
            "drivelevel_price" => $data['drivelevel_price'] * 100,
            "drivelevel_priceday" => $data['drivelevel_priceday'] * 100,
        ];
        $update = db("drivelevel")->where('drivelevel_id', $data['drivelevel_id'])->update($upd);
        if ($update) $this->ajaxReturn(200, "修改成功");
        else $this->ajaxReturn(603, "修改失败");

    }

    //车辆价格设置
    public function carprice()
    {
        if (request()->isGet()) {
            return $this->fetch('carprice');
        }
        $page = input('page');
        $limit = input('limit');
        $where = [
            'v.type' => 1
        ];
        $field = "v.vehicle_rent_id,v.vehicle_price,v.vehicle_priceday,concat(ca.cartype_what,c.cartype_what) as name";
        $list = model('Vehiclerent')->getVehicleRentList($field, $page, $limit, $where);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['vehicle_price'] = $list['data'][$k]['vehicle_price'] / 100;
            $list['data'][$k]['vehicle_priceday'] = $list['data'][$k]['vehicle_priceday'] / 100;
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //添加车型价格
    public function addCarType()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $this->assign('cartype', $cartype);
            return $this->fetch('addcartype');
        }
        $data = input();
        $data['vehicle_price'] = $data['vehicle_price'] * 100;
        $data['vehicle_priceday'] = $data['vehicle_priceday'] * 100;
        $count = db('vehicle_rent')->where(['cartype_id' => $data['cartype_id'], 'type' => 1])->count();
        if ($count) $this->ajaxReturn(603, "该车型已存在！");
        $res = db('vehicle_rent')->insert($data);
        if ($res) $this->ajaxReturn(200, "添加成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //修改车型价格
    public function updCarType()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $id = input('id');
            $data = db('vehicle_rent')->where('vehicle_rent_id', $id)->find();
            $data['vehicle_price'] = $data['vehicle_price'] / 100;
            $data['vehicle_priceday'] = $data['vehicle_priceday'] / 100;
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $data['pid'] = db('cartype')->where('cartype_id', $data['cartype_id'])->value('pid');
            $carTwoType = model('carmanage')->gettypelist(['pid' => ['not in', 0]]);
            $this->assign('data', $data);
            $this->assign('cartype', $cartype);
            $this->assign('carTwoType', $carTwoType);
            return $this->fetch('updcartype');
        }
        $data = input();
        $data['vehicle_price'] = $data['vehicle_price'] * 100;
        $data['vehicle_priceday'] = $data['vehicle_priceday'] * 100;
        $res = db('vehicle_rent')->update($data);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //清空车型价格
    public function delCarPrice()
    {
        $id = input('id');
        $res = db('vehicle_rent')->where('vehicle_rent_id', $id)->update(['vehicle_price' => null, 'vehicle_priceday' => null]);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //车辆级驾驶员价格设置
    public function drivecarprice()
    {
        if (request()->isGet()) {
            return $this->fetch('drivecarprice');
        }
        $field = "concat(ca.cartype_what,c.cartype_what) as name,dl.drive_level,dc.driver_car_price,dc.driver_car_priceday,dc.driver_car_id";
        $where = [
            'dc.type' => 1,
        ];
        $list = model('DriverCar')->getDriveCarPrice($field, $where);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['driver_car_price'] = $list['data'][$k]['driver_car_price'] / 100;
            $list['data'][$k]['driver_car_priceday'] = $list['data'][$k]['driver_car_priceday'] / 100;
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //添加车及驾驶员价格
    public function addCarAndDrive()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $data = db('driving_licence')->where('status', 1)->select();
            $this->assign('cartype', $cartype);
            $this->assign('data', $data);
            return $this->fetch('addcaranddrive');
        }
        $data = input();
        $data['driver_car_price'] = $data['driver_car_price'] * 100;
        $data['driver_car_priceday'] = $data['driver_car_priceday'] * 100;
        $count = db('driver_car')->where(['driving_licence_id' => $data['driving_licence_id'], 'cartype_id' => $data['cartype_id'], 'type' => 1])->count();
        if ($count) $this->ajaxReturn(603, "此类型组合已存在！");
        $res = db('driver_car')->insert($data);
        if ($res) $this->ajaxReturn(200, "添加成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //修改车及驾驶员价格
    public function updCarAndDrive()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $id = input('id');
            $data = db('driver_car')->where('driver_car_id', $id)->find();
            $data['driver_car_price'] = $data['driver_car_price'] / 100;
            $data['driver_car_priceday'] = $data['driver_car_priceday'] / 100;
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $data['pid'] = db('cartype')->where('cartype_id', $data['cartype_id'])->value('pid');
            $carTwoType = model('carmanage')->gettypelist(['pid' => ['not in', 0]]);
            $driving = db('driving_licence')->where('status', 1)->select();
            $this->assign('driving', $driving);
            $this->assign('data', $data);
            $this->assign('cartype', $cartype);
            $this->assign('carTwoType', $carTwoType);
            return $this->fetch('updcaranddrive');
        }
        $data = input();
        $data['driver_car_price'] = $data['driver_car_price'] * 100;
        $data['driver_car_priceday'] = $data['driver_car_priceday'] * 100;
        $res = db('driver_car')->update($data);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //清空车及驾驶员价格
    public function delCarAndDrive()
    {
        $id = input('id');
        $res = db('driver_car')->where('driver_car_id', $id)->update(['driver_car_price' => null, 'driver_car_priceday' => null]);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

}